<?php
session_start();
require_once('config.php');
if($_POST['tinhthanh']=='tinhthanh'){
	$quanhuyen = $tc->quan_huyen($_POST['id']);
	$view .= '<option value="">Quận - Huyện</option>';
	while($row = mysql_fetch_array($quanhuyen)){
		if($row['id'] != $_POST['check']) $view .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
		else $view .= '<option value="'.$row['id'].'" selected="selected">'.$row['name'].'</option>';
	}
	echo $view;
	mysql_free_result($quanhuyen);
}
if($_POST['checkemail']=='checkemail'){
	$email = $_POST['email'];
	$total = mysql_result($tc->KiemTraEmail($email),0);
	$pattern = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$';
	if(eregi($pattern, $email) && $total==0) echo '<span class="require">Email hợp lệ</span>'; else echo '<span class="require">Email đã có người dùng hoặc chưa đúng</span>';
}
if($_POST['news']=='news'){
	$news = $tc->view_wall_new($_POST['id']);
	while($row_news = mysql_fetch_array($news)){
		$view_news .= '<div class="item"><p><a href="'.$row_news['user_id'].'/"><img src="'.url_hinh_user_40x40.$row_news['avatar'].'.jpg" alt="" /><b>'.$row_news['name'].'</b></a> <a href="index.html?user='.$row_news['user_id'].'&type=detail&id='.$row_news['info_id'].'" style="color:#666">'.$row_news['status'].'</a></p><hr /></div>';
	}
	echo $view_news;
	mysql_free_result($news);
}
if($_POST['slider']=='slider'){
	$tuanthu = $_POST['id'];
	
	$info_tuanthai = array();
	$slider = $tc->slider();
	while($row_slider = mysql_fetch_array($slider)){
		$info_tuanthai[$row_slider['id']] = $row_slider['info'];
	}
	mysql_free_result($slider);
	
	if($tuanthu!=1 && $tuanthu!=38){
		$view_slider .= '<div class="slider_ac" style="left:50px"><div class="nen '.($tuanthu-1).'"><div class="view_tuan" style="left:0">Tuần thai thứ '.($tuanthu-1).'</div><div class="info_tuan">'.$info_tuanthai[$tuanthu-1].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-'.($tuanthu-1).'.jpg" alt="Tuần thai thứ '.($tuanthu-1).'"></div>';
		$view_slider .= '<div class="slider_center"><div class="info">'.$info_tuanthai[$tuanthu].'</div><img src="'.url_hinh_slider.'tuan-thai-thu-'.$tuanthu.'.jpg" alt="Tuần thai thứ '.$tuanthu.'"></div>';
		$view_slider .= '<div class="slider_ac" style="right:50px"><div class="nen '.($tuanthu+1).'"><div class="view_tuan" style="right:0">Tuần thai thứ '.($tuanthu+1).'</div><div class="info_tuan">'.$info_tuanthai[$tuanthu+1].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-'.($tuanthu+1).'.jpg" alt="Tuần thai thứ '.($tuanthu+1).'"></div>';
	}elseif($tuanthu==1){
		$view_slider .= '<div class="slider_ac" style="left:50px"><div class="nen 38"><div class="view_tuan" style="left:0">Tuần thai thứ 38</div><div class="info_tuan">'.$info_tuanthai[38].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-38.jpg" alt="Tuần thai thứ 38"></div>';
		$view_slider .= '<div class="slider_center"><div class="info">'.$info_tuanthai[1].'</div><img src="'.url_hinh_slider.'tuan-thai-thu-1.jpg" alt="Tuần thai thứ 1"></div>';
		$view_slider .= '<div class="slider_ac" style="right:50px"><div class="nen 2"><div class="view_tuan" style="right:0">Tuần thai thứ 2</div><div class="info_tuan">'.$info_tuanthai[2].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-2.jpg" alt="Tuần thai thứ 2"></div>';
	}elseif($tuanthu==38){
		$view_slider .= '<div class="slider_ac" style="left:50px"><div class="nen 37"><div class="view_tuan" style="left:0">Tuần thai thứ 37</div><div class="info_tuan">'.$info_tuanthai[37].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-37.jpg" alt="Tuần thai thứ 37"></div>';
		$view_slider .= '<div class="slider_center"><div class="info">'.$info_tuanthai[38].'</div><img src="'.url_hinh_slider.'tuan-thai-thu-38.jpg" alt="Tuần thai thứ 38"></div>';
		$view_slider .= '<div class="slider_ac" style="right:50px"><div class="nen 1"><div class="view_tuan" style="right:0">Tuần thai thứ 1</div><div class="info_tuan">'.$info_tuanthai[1].'</div></div><img src="'.url_hinh_slider.'tuan-thai-thu-1.jpg" alt="Tuần thai thứ 1"></div>';
	}else return false;
	echo $view_slider;
}
if($_POST['image_wall']=='image_wall') echo '<img src="'.url_hinh_photo_full.$_POST['id'].'.jpg" alt="'.$_POST['id'].'" />';// xem ảnh trên wall

if($_POST['resetpass']=='resetpass'){
	$email = $_POST['email'];
	$pattern = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$';
	if(eregi($pattern, $email)){
		echo $tc->resetpass($email);
	}else echo 4;
}
if($_POST['resetpassword']=='resetpassword'){
	$pass1 = $_POST['pass1'];
	$pass2 = $_POST['pass2'];
	$key = $_POST['id'];
	
	if(strlen($pass1)<6) echo 4;
	elseif($pass1!=$pass2) echo 3;
	else{
		if($tc->check_reset($key) == 1){
			$tc->thaydoimatkhau($pass1,$key);
			echo 1;
		}else echo 2;
	}
}

if(@$_SESSION['idUser']){
	if($_POST['wall_content']=='wall_content'){
		$content = str_replace('Bé thế nào rồi...','',$_POST['content']);
		$content = str_replace('B&eacute; thế n&agrave;o rồi...','',$_POST['content']);
		$content = str_replace('&nbsp;',' ',$content);
		$content = str_replace('<br /> <br />',' ',$content);
		$content = str_replace('  ',' ',$content);
		$content = trim($content,'<br />');
		$content = trim($content);
		if(($_POST['share']==1 || $_POST['share']==3) && ($content!='' || $_POST['photo']!='')){//insert wall
			$share = $_POST['share'];
			$photo_name = $_POST['photo'];
			$user = $_POST['user'];
			$tc->insert_wall_status($content,$share,$photo_name,$user);
			echo '1';
		}
	}
	
	if($_POST['comment']=='comment'){
		$id = $_POST['id'];
		$cm = trim($_POST['cm']);
		if($id>1 && $cm!='') {
			$comment = $tc->insert_comment($id,$cm);
			return true;
		}else return false;
	}// end comment
	if($_POST['comment_photo']=='comment_photo'){
		$id = $_POST['id'];
		$cm = trim($_POST['cm']);
		if($id>1 && $cm!=''){
			$comment = $tc->insert_comment_photo($id,$cm);
			return true;
		}else return false;
	}// end comment
	
	if($_POST['create_album']=='create_album') $tc->create_album($_POST['name_album'],$_POST['share_id']);
	
	if($_POST['anhdaidien']=='anhdaidien') $tc->anhdaidien_album($_POST['id']);
	
	if($_POST['setalbum']=='setalbum') $tc->set_anhdaidien_album($_POST['name'],$_POST['album']);
	
	if($_POST['friend_xacnhan']=='friend_xacnhan') $tc->friend_xacnhan($_POST['id']);
	
	if($_POST['friend_huy']=='friend_huy') $tc->friend_huy($_POST['id']);
	
	if($_POST['search_friend']=='search_friend'){
		$str = trim($_POST['str']);
		if($str!=''){
			$search = $tc->search($str);
			$total = mysql_num_rows($search);
			while($row_s = mysql_fetch_array($search)){
				$view_search .= '<div class="item"><a href="index.html?user='.$row_s['id'].'"><img src="'.url_hinh_user_40x40.$row_s['avatar'].'.jpg" alt="'.$row_s['name'].'" />'.$row_s['name'].'</a></div>';
			}
			if($total>10) $view_search .= '<div class="item" style="background-color:#FFEAF1; text-align:center"><a href="index.html?user='.$_SESSION['idUser'].'&type=friend&search='.$str.'">Xem thêm kết quả</a></div>';
			echo $view_search;
			mysql_free_result($search);
		}
	}
	
	if($_POST['video_comment']=='video_comment'){
		$id = $_POST['id'];
		$cm = str_replace('Viết bình luận...','',$_POST['cm']);
		$cm = trim($cm);
		$date = date('Y-m-d H:i:s');
		if($id>0 && $cm!='') mysql_query("INSERT INTO articles_comment VALUES ('','{$id}','{$cm}','".$_SESSION['idUser']."','{$date}')");
		else echo '0';
	}
	
	if($_POST['deleteinfo']=='deleteinfo' && $_POST['user']==$_SESSION['idUser']) $tc->info_delete(str_replace('delete','',$_POST['id']));
}
?>